﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;

namespace ProductManagement
{
    public partial class ReportChiTiet : Form
    {
        private int maDaiLy;
        private String tenDaiLy;
        private int ngay, thang, nam;
        public ReportChiTiet()
        {
            InitializeComponent();
            maDaiLy = 0;
        }
        public ReportChiTiet(int _maDaiLy, String daiLy, int ng, int th, int na)
        {
            InitializeComponent();
            maDaiLy = _maDaiLy;            
            tenDaiLy = daiLy;
            Text = tenDaiLy;
            ngay = ng;
            thang = th;
            nam = na;
        }
        private void LoadNam()
        {
            String strSql = "select distinct year(ngay)&'' as Nam from GiaoHang";
            DataTable dt = DataAccess.Query(strSql);
            DataRow aRow = dt.NewRow();
            aRow["Nam"] = "Tất cả";
            dt.Rows.InsertAt(aRow, 0);
            cboNam.DataSource = dt;
            cboNam.DisplayMember = "Nam";
            cboNam.ValueMember = "Nam";
            if (nam == 0)
                cboNam.SelectedIndex = 0;
            else
                cboNam.SelectedValue = nam;
;
        }
        private void LoadThang()
        {
            if (cboNam != null)
            {
                int n = 12;
                if (cboNam.SelectedValue == "Tất cả")
                    n = 0;
                DataTable dt = new DataTable();
                dt.Columns.Add("MaThang", typeof(int));
                dt.Columns.Add("TenThang", typeof(String));
                DataRow aRow = dt.NewRow();
                aRow["MaThang"] = 0;
                aRow["TenThang"] = "Tất cả";
                dt.Rows.Add(aRow);
                for (int i = 0; i < n; i++)
                {
                    aRow = dt.NewRow();
                    aRow["MaThang"] = i + 1;
                    aRow["TenThang"] = (i + 1);
                    dt.Rows.Add(aRow);
                }
                cboThang.DataSource = dt;
                cboThang.DisplayMember = "TenThang";
                cboThang.ValueMember = "MaThang";
                cboThang.SelectedValue = thang;
                if(cboThang.SelectedIndex < 0)
                    cboThang.SelectedIndex = 0;
            }
        }
        private void LoadNgay()
        {
            if (cboThang.SelectedValue != null)
            {
                int[] ngayThang = new int[] { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                if ((int)cboThang.SelectedValue>0 && DateTime.IsLeapYear(int.Parse(cboNam.SelectedValue.ToString())))
                    ngayThang[2] = 29;
                DataTable dt = new DataTable();
                dt.Columns.Add("MaNgay", typeof(int));
                dt.Columns.Add("TenNgay", typeof(String));
                DataRow aRow = dt.NewRow();
                aRow["MaNgay"] = 0;
                aRow["TenNgay"] = "Tất cả";
                dt.Rows.Add(aRow);
                for (int i = 0; i < ngayThang[(int)cboThang.SelectedValue]; i++)
                {
                    aRow = dt.NewRow();
                    aRow["MaNgay"] = i + 1;
                    aRow["TenNgay"] = i + 1;
                    dt.Rows.Add(aRow);
                }
                cboNgay.DataSource = dt;
                cboNgay.ValueMember = "MaNgay";
                cboNgay.DisplayMember = "TenNgay";
                cboNgay.SelectedValue = ngay;
                if(cboNgay.SelectedIndex < 0)
                    cboNgay.SelectedIndex = 0;
            }
        }
        private void LoadReport()
        {
            String dkNgay = "";
            String title = "";
            String strSql;
            DataTable dt = new DataTable();
            if (cboNam.SelectedValue != "Tất cả")
            {
                dkNgay = " and year(gh.Ngay)='" + cboNam.SelectedValue + "'";
                if ((int)cboThang.SelectedValue != 0)
                {
                    dkNgay += " and month(gh.Ngay)='" + cboThang.SelectedValue + "'";
                    if ((int)cboNgay.SelectedValue != 0)
                    {
                        dkNgay += " and day(gh.Ngay)='" + cboNgay.SelectedValue + "'";
                        title += cboNgay.SelectedValue + "/";
                    }
                    title += cboThang.SelectedValue + "/";
                }
                title += cboNam.SelectedValue;
            }
            else
            {
                try
                {
                    strSql = "select min(Ngay) as NgayMin, max(Ngay) as NgayMax from PhanPhoi where DaiLy=" + maDaiLy;
                    dt = DataAccess.Query(strSql);
                    title += "từ " + ((DateTime)dt.Rows[0]["NgayMin"]).Month + "/" + ((DateTime)dt.Rows[0]["NgayMin"]).Year +
                        " đến " + ((DateTime)dt.Rows[0]["NgayMax"]).Month + "/" + ((DateTime)dt.Rows[0]["NgayMax"]).Year;
                }
                catch (Exception ex) { }
            }
            strSql = "SELECT gas.TenLoai as LoaiGas, pp.SoLuong as SoLuong, pp.GiaGoc as GiaGoc, pp.GiaBan as GiaBan, (pp.GiaBan-pp.GiaGoc)*pp.SoLuong as Lai, gh.Ngay as Ngay " +
                            "from PhanPhoi pp, LoaiGas gas, GiaoHang gh where pp.MaGiaoHang=gh.ID and pp.LoaiGas=gas.ID and DaiLy=" + maDaiLy + dkNgay + " order by gh.Ngay";
            dt = DataAccess.Query(strSql);
            dt.Columns.Add("STT");
            double tongLai = 0;
            int tongSL = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["STT"] = i + 1;
                tongLai += (int)dt.Rows[i]["Lai"];
                tongSL += (int)dt.Rows[i]["SoLuong"];
            }
            DataRow sumRow = dt.NewRow();
            sumRow["LoaiGas"] = "Tổng: ";
            sumRow["Lai"] = tongLai;
            sumRow["SoLuong"] = tongSL;
            dt.Rows.Add(sumRow);
            dgvPhanPhoi.AutoGenerateColumns = false;
            dgvPhanPhoi.DataSource = dt;
            System.Drawing.Font font = new Font("Arial", 9.75f, FontStyle.Bold);
            for (int i = 1; i < dgvPhanPhoi.Rows[0].Cells.Count; i++)
                dgvPhanPhoi.Rows[dgvPhanPhoi.Rows.Count - 1].Cells[i].Style.Font = font;
            dgvPhanPhoi.Rows[dgvPhanPhoi.Rows.Count - 1].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            strSql = "Select gas1.TenLoai as VoPhanPhoi1, gas.TenLoai as VoThuVe1, th.SoLuong as SoLuong1, th.PhiDoiVo as PhiDoiVo1, th.PhiDoiVo*th.SoLuong as TongTien1, gh.Ngay as Ngay1 " +
                    "from ((ThuHoi th left join LoaiGas gas on th.VoThuVe=gas.ID) left join LoaiGas gas1 on th.VoPhanPhoi=gas1.ID) left join GiaoHang gh on th.MaGiaoHang=gh.ID where DaiLy=" + maDaiLy + dkNgay + " order by gh.Ngay";
            dt = DataAccess.Query(strSql);
            dt.Columns.Add("STT1");
            double tongTien = 0;
            tongSL = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["STT1"] = i + 1;
                tongTien += (int)dt.Rows[i]["TongTien1"];
                tongSL += (int)dt.Rows[i]["SoLuong1"];
                if (dt.Rows[i]["VoThuVe1"] == System.DBNull.Value)
                    dt.Rows[i]["VoThuVe1"] = "Thế chân";
            }
            sumRow = dt.NewRow();
            sumRow["VoThuVe1"] = "Tổng: ";
            sumRow["TongTien1"] = tongTien;
            sumRow["SoLuong1"] = tongSL;
            dt.Rows.Add(sumRow);
            dgvThuHoi.AutoGenerateColumns = false;
            dgvThuHoi.DataSource = dt;
            for (int i = 1; i < dgvThuHoi.Rows[0].Cells.Count; i++)
                dgvThuHoi.Rows[dgvThuHoi.Rows.Count - 1].Cells[i].Style.Font = font;
            dgvThuHoi.Rows[dgvThuHoi.Rows.Count - 1].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;            
            lblPhanPhoi.Text = "Bảng phân phối Gas " + title;
            lblThuHoi.Text = "Bảng thu hồi vỏ bình Gas " + title;
        }
        private void ReportChiTiet_Load(object sender, EventArgs e)
        {            
            LoadNam();
            LoadThang();
            LoadNgay();
            LoadReport();
            Text = "Đại lý " + tenDaiLy;
        }

        private void ReportChiTiet_SizeChanged(object sender, EventArgs e)
        {
            dgvPhanPhoi.Height = (Height - 180) / 2;
            dgvThuHoi.Location = new Point(dgvThuHoi.Location.X, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 40);
            dgvThuHoi.Height = (Height - 180) / 2;
            lblThuHoi.Location = new Point(dgvThuHoi.Location.X, dgvPhanPhoi.Location.Y + dgvPhanPhoi.Height + 20);
        }

        private void dgvPhanPhoi_Sorted(object sender, EventArgs e)
        {
            System.Drawing.Font font = new Font("Arial", 9.75f, FontStyle.Bold);
            for (int i = 1; i < dgvPhanPhoi.Rows[0].Cells.Count; i++)
                dgvPhanPhoi.Rows[dgvPhanPhoi.Rows.Count - 1].Cells[i].Style.Font = font;
            dgvPhanPhoi.Rows[dgvPhanPhoi.Rows.Count - 1].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

        private void dgvThuHoi_Sorted(object sender, EventArgs e)
        {
            System.Drawing.Font font = new Font("Arial", 9.75f, FontStyle.Bold);
            for (int i = 1; i < dgvThuHoi.Rows[0].Cells.Count; i++)
                dgvThuHoi.Rows[dgvThuHoi.Rows.Count - 1].Cells[i].Style.Font = font;
            dgvThuHoi.Rows[dgvThuHoi.Rows.Count - 1].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

        private void cboNam_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                LoadThang();
                LoadReport();
            }
            catch (Exception ex) { }
        }

        private void cboThang_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                LoadNgay();
                LoadReport();
            }
            catch (Exception ex) { }
        }

        private void cboNgay_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                LoadReport();
            }
            catch (Exception ex) { }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
